package com.dk.service.impl;

import com.dk.mapper.SslPersonMapper;
import com.dk.mapper.SslPersonRoleMapper;
import com.dk.pojo.SslPerson;
import com.dk.pojo.SslPersonExample;
import com.dk.pojo.SslPersonRole;
import com.dk.pojo.SslPersonRoleExample;
import com.dk.service.SslPersonService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.Ssl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@DubboService
@Service
public class SslPersonServiceImpl implements SslPersonService {
    @Autowired
    private SslPersonMapper personMapper;
    @Autowired
    private SslPersonRoleMapper personRoleMapper;

    public SslPerson findPersonByName(String pname) {
        SslPersonExample example = new SslPersonExample();
        SslPersonExample.Criteria criteria = example.createCriteria();
        criteria.andPnameEqualTo(pname);
        List<SslPerson> list = personMapper.selectByExample(example);
        return (list ==null || list.isEmpty())?null:list.get(0);
    }
    @Override
    public List<SslPerson> findPersonList() {

        return personMapper.selectByExample(new SslPersonExample());
    }

    /**
     * 保存用户信息
     */
    @Override
    public void savePerson(SslPerson pseron, Integer[] roleIds, String salt, String s) {
        pseron.setPwd(s);
        pseron.setSalt(salt);
		//1.2添加用户，并返回id
		personMapper.insertSelective(pseron);
		//2、维护第三张表
		//2.1、定义集合用来接收第三张表的数据
		List<SslPersonRole> prs = new ArrayList<>();
		Integer psersonId = pseron.getPid();
		//2.2、遍历角色id，组装数据
		for(Integer roleId : roleIds){
			SslPersonRole pr = new SslPersonRole();
			pr.setPersonId(psersonId);
			pr.setRoleId(roleId);
			prs.add(pr);
		}
		//调用mapper进行批量添加
		int result =  personRoleMapper.addPersonRoleBatch(prs);
		System.out.println(result);

    }

    @Override
    public SslPerson findPersonById(Integer pid) {

        return personMapper.selectByPrimaryKey(pid);
    }

    @Override
    public void updatePerson(SslPerson pseron, Integer[] roleIds) {
        //1、修改用户信息
        personMapper.updateByPrimaryKeySelective(pseron);
        //2、删除用户角色中间表数据
        SslPersonRoleExample example = new SslPersonRoleExample();
        //全限定名
        SslPersonRoleExample.Criteria criteria = example.createCriteria();
        criteria.andPersonIdEqualTo(pseron.getPid());
        personRoleMapper.deleteByExample(example);
        //3、重新维护中间表
        List<SslPersonRole> prs = new ArrayList<>();
        Integer psersonId = pseron.getPid();
        //2.2、遍历角色id，组装数据
        for(Integer roleId : roleIds){
            SslPersonRole pr = new SslPersonRole();
            pr.setPersonId(psersonId);
            pr.setRoleId(roleId);
            prs.add(pr);
        }
        //调用mapper进行批量添加
        int result =  personRoleMapper.addPersonRoleBatch(prs);
        System.out.println(result);
    }

    @Override
    public void deletePerson(Integer pid) {
        //1、删除用户
        personMapper.deleteByPrimaryKey(pid);
        //2、根据用户id，删除用户角色中间表信息
        SslPersonRoleExample example = new SslPersonRoleExample();
        //全限定名
        SslPersonRoleExample.Criteria criteria = example.createCriteria();
        criteria.andPersonIdEqualTo(pid);
        personRoleMapper.deleteByExample(example);
    }
}
